﻿
using BulletSharp;
using UnityEngine;
using ETHotfix;
using ETModel;
using System;
using System.Collections.Generic;
using System.Text;
using ZGameSurvival.Hotfix.Entitys;
using ZGameSurvival.Hotfix.Components;

namespace ZGameSurvival.Hotfix {
    [ObjectSystem]
    public class GameSceneMgrDestroy : DestroySystem<HotfixComponent> {
        public override void Destroy(HotfixComponent self) {
            if (StartConfigComponent.Instance.StartConfig.AppType.HasFlag(AppType.Map)) {
                GameSceneMgr.Destroy();
            }
        }
    }
    [ObjectSystem]
    public class GameSceneMgrStart : StartSystem<HotfixComponent> {
        public override void Start(HotfixComponent self) {
            if (StartConfigComponent.Instance.StartConfig.AppType.HasFlag(AppType.Map)) {
                GameSceneMgr.Start();
            }
        }
    }
    /// <summary>
    /// 场景资源,只有Map服务器可用
    /// </summary>
    public static class GameSceneMgr {
        public static GameUnit Ground;
        public static void Start() {

            //加入寻路组件,以及地图的寻路地图(因为只有一个地图,直接放在总场景下)
            if (Game.Scene.GetComponent<PathfindingComponent>() == null) Game.Scene.AddComponent<PathfindingComponent, string>("./Res/TerrainLTGraph.bytes");

            //加载地形
            if (Ground == null) {
                var groundCfg = Game.Scene.GetComponent<ConfigComponent>().Get<UnitConfig>(5002);//地形-村庄平原

                var pos = new Vector3(0, 0, 0);

                Ground = ComponentFactory.CreateWithId<GameUnit, long>(IdGenerater.GenerateId(), 0);
                Ground.UnitConfigId = groundCfg.Id;
                Ground.Center = groundCfg.GetCenter();
                Ground.SetPosition(pos);
                //地图的地形作为物理形状
                Ground.AddComponent<BulletCollisionShape, CollisionShape>(groundCfg.CreateBulletCollisionShape());
                Ground.AddComponent<BulletCollisionObject, float, Vector3, Quaternion>(0, Ground.GetPosition(), Ground.GetRotation());

                //将地形加入客户端调试线框
                //Game.Scene.GetComponent<DebugInfoSyncComponent>()?.UpdateGizmosMeshSync(Ground.Id, Ground.BuildUnitInfo());
            }

            Log.Info("资源完成初始化加载!");

            //默认资源等加载都放这
        }
        public static void Destroy() {
        }
    }
}
